<template>
  <div>
    <!-- 组件 面包屑-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/admin/category/list">类别管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>添加类别</el-breadcrumb-item>
    </el-breadcrumb>

    <h3 style="margin: 20px 0;">添加类别</h3>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" class="demo-ruleForm">

      <el-form-item label="父级类别id" prop="parentId">
        <el-input v-model="ruleForm.parentId"></el-input>
      </el-form-item>

      <el-form-item label="类别名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="排序值" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="类别图标的URL" prop="icon">
        <el-input v-model="ruleForm.icon"></el-input>
      </el-form-item>
      <el-form-item label="关键词" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>

      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            active-color="#13ce66"
            inactive-color="#999"
            active-value="1"
            inactive-value="0">
        </el-switch>
      </el-form-item>

      <el-form-item label="是否显示导航栏" prop="display">
        <el-switch
            v-model="ruleForm.display"
            active-color="#13ce66"
            inactive-color="#999"
            active-value="1"
            inactive-value="0">
        </el-switch>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        parentId:'',
        name: '',
        icon: '',
        enable: true,
        display: true,
        keywords: '',
        sort: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入类别名称', trigger: 'blur' },
          { min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' }
        ],
        sort:[ // 可以通过正则表达式进行验证
          {pattern: /^[0-9][0-9]?$/,message: '必须是0-99之间的数值', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          let url = "http://localhost:9999/categories/add-new";
          this.axios.post(url,this.ruleForm).then((response)=>{
            console.log(response);
            let jsonResult = response.data;
            console.log(jsonResult);

            if(response.data.code == 20000){
              this.$message({
                message: '添加类别成功',
                type: 'success'
              });
            }else{
              this.$message.error(response.data.message);
            }

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

